# -*- UTF-8 -*-

import pymongo
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import time

def mongdb2es:
    # MongDB
    client = pymongo.MongoClient(host='10.1.1.9', port=50000)
    # ES
    es = Elasticsearch(['http://10.1.1.9:9266'])

    client.list_database_names()
    db = client.KnowledgeBase
    collection = db.unit
    objs = collection.find()
    collection.count()

    start = time.time()
    count = 0
    actions = []
    for obj in objs:
        action1 = {"_index": "title",
                   "_type": "title",
                   "_source": {
                       "Title": obj['Title']
                   }
                   }
        actions.append(action1)
        count = count + 1
        if count % 30000 == 0:
            a = helpers.bulk(es, actions)
            end = time.time()
            print("{} {}s".format(a, end - start))
            actions = []

    # 最终提交
    a = helpers.bulk(es, actions)
    end = time.time()
    print("All {} {} s".format(a, end - start))

if __name__ == '__main__':
    print("Hello World!!!!")